package swing.game.CatchTheRat;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.BoundedRangeModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class CatchTheRat {

	private static int score = 0;
	private static Timer t = null;

	// define JLabel class that fit the image icon to the label
	static class MyJLabel extends JLabel {
		ImageIcon imageIcon;

		public MyJLabel(ImageIcon icon) {
			super();
			this.imageIcon = icon;
		}

		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(imageIcon.getImage(), 0, 0, getWidth(), getHeight(),
					this);
		}
	}

	// end of class

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Catch The Rat Game");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				JPanel contentPane = (JPanel) frame.getContentPane();

				ImageIcon icon = new ImageIcon("bg.png");
				final JLabel lbGround = new MyJLabel(icon);
				final JLabel lbRat = new JLabel(new ImageIcon("Rat-icon.png"));

				final ActionListener timerAction = new ActionListener() {

					Random r = new Random();

					@Override
					public void actionPerformed(ActionEvent e) {
						lbRat.setLocation(r.nextInt(lbGround.getWidth() - 10),
								r.nextInt(lbGround.getHeight() - 10));
					}
				};
				//
				t = new Timer(600, timerAction);
				ChangeListener changed = new ChangeListener() {

					@Override
					public void stateChanged(ChangeEvent e) {
						Object source = e.getSource();
						if (source instanceof BoundedRangeModel) {
							BoundedRangeModel aModel =
								(BoundedRangeModel) source;

							t.setDelay(aModel.getValue() * 10);
						}
					}
				};
				t.start();

				// declare a slider for changed the speed of the rat
				JSlider slider = new JSlider(0, 100, 20);
				BoundedRangeModel model = slider.getModel();
				model.addChangeListener(changed);
				JPanel ratePane = new JPanel();
				JLabel rate = new JLabel("Changed Rate : ");

				ratePane.add(rate, BorderLayout.WEST);
				ratePane.add(slider, BorderLayout.CENTER);
				contentPane.add(lbGround, BorderLayout.CENTER);
				contentPane.add(ratePane, BorderLayout.SOUTH);
				lbGround.setLayout(new FlowLayout());
				lbGround.add(lbRat);

				// ******* CHANGED CURSOR ICON
				Toolkit kit = Toolkit.getDefaultToolkit();
				Image image = kit.getImage("plus.gif");
				Cursor c =
					kit.createCustomCursor(image, new Point(lbGround.getX(),
							lbGround.getY()), "img");
				lbGround.setCursor(c);
				// ***********end of changed cursor

				JPanel northPane = new JPanel();
				JLabel lbScore = new JLabel("YOUR SCORE :");
				final JLabel result = new JLabel();
				northPane.add(lbScore, BorderLayout.CENTER);
				northPane.add(result, BorderLayout.WEST);
				contentPane.add(northPane, BorderLayout.NORTH);

				lbRat.addMouseListener(new MouseAdapter() {

					@Override
					public void mousePressed(MouseEvent e) {
						score++;
						result.setText(Integer.toString(score));
						Toolkit.getDefaultToolkit().beep();
					}

				});
				frame.setLocationByPlatform(true);
				frame.setResizable(false);
				frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}

}
